﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing.Imaging;
using System.Drawing;
using System.Windows.Forms;

namespace LocalizationTest.Tools
{
  class DrawingTools
  {
    public static void CreateMetaFileGraphics(string emfFileName, out Metafile mf, out Graphics graphics)
    {
      // Create a Bitmap
      Bitmap bitmap = new Bitmap(1, 1, PixelFormat.Format24bppRgb);
      // Wrap a Graphics around the Bitmap
      Graphics gRef = Graphics.FromImage(bitmap);
      // Get an HDC from the Graphics
      IntPtr hDC = gRef.GetHdc();
      System.IO.File.Copy(DrawingTools.GetPath() + "dummy.emf", emfFileName);
      // Create a recordable Metafile
      mf = new Metafile(emfFileName, hDC);
      // Release the HDC
      gRef.ReleaseHdc(hDC);
      // Wrap a Graphics around the Metafile so we can draw on it
      graphics = Graphics.FromImage(mf);
      graphics.ScaleTransform(1, -1);
    }
    public static void FinishMetaFileGraphics(Metafile mf, Graphics graphics)
    {
      Pen blackPen = new Pen(Color.Black, 2);
      graphics.DrawRectangle(blackPen, -600, -600, 1200, 1200);
      mf.Dispose();
    }
    public static void DrawFlippedText(Graphics graphics, String strText, Font font, Brush brush, float fX, float fY)
    {
      // Get size of drawn text in Pixels
      //
      SizeF sizeText = graphics.MeasureString(strText, font, 2000);
      int width = Convert.ToInt32(sizeText.Width);
      int height = Convert.ToInt32(sizeText.Height);

      //
      // Make a bitmap make it transparent (uses white as transparencycolor !)
      // draw the string in it
      // then flip that image.
      //
      Bitmap bmp = new Bitmap(width, height);
      bmp.MakeTransparent(Color.White);
      Graphics grfBmp = Graphics.FromImage(bmp);
      grfBmp.DrawString(strText, font, brush, 0, 0);
      bmp.RotateFlip(RotateFlipType.RotateNoneFlipY);

      //
      // Draw the bitmap into the page.
      //
      /*
      Rectangle rect = new Rectangle(Convert.ToInt32(fX - width / 2),
      Convert.ToInt32(fY - height / 2),
      Convert.ToInt32(sizeText.Width),
      Convert.ToInt32(sizeText.Height));
       */
      Rectangle rect = new Rectangle(Convert.ToInt32(fX),
      Convert.ToInt32(fY - height / 2),
      Convert.ToInt32(sizeText.Width),
      Convert.ToInt32(sizeText.Height));
      graphics.DrawImage(bmp, rect, 0, 0, width, height,
      GraphicsUnit.Pixel);
    }
    public static Color GetColor(int index)
    {
      switch (index)
      {
        case 0: return Color.Red;
        case 1: return Color.Green;
        case 2: return Color.Blue;
        case 3: return Color.Yellow;
        case 4: return Color.Cyan;
        case 5: return Color.Pink;
        case 6: return Color.Brown;
        case 7: return Color.Orange;
        case 8: return Color.LemonChiffon;
        case 9: return Color.Lime;
        default: return Color.Gray;
      }
    }
    public static string GetVideoOutputPath()
    {
      string executablePath = Application.ExecutablePath;
      return executablePath.Remove(executablePath.IndexOf("\\bin\\")) + "\\video-output\\";
    }
    public static string GetPath()
    {
      string executablePath = Application.ExecutablePath;
      return executablePath.Remove(executablePath.IndexOf("\\bin\\")) + "\\";
    }
    public static Color GetShadedColor(double shade, ref Color color)
    {
      Color color2 = Color.FromArgb(
        (int)(color.R + (220 - color.R) * shade),
        (int)(color.G + (220 - color.G) * shade),
        (int)(color.B + (220 - color.B) * shade));
      return color2;
    }

    public static Color GetColorForSensor(Sensor sensor)
    {
      if (sensor is BearingSensor)
      {
        return MapWithLandmarks.GetColorOfLandmark((sensor as BearingSensor).landmarkID);
      }
      if (sensor is BearingSensorAmbiguous)
      {
        return Color.DarkBlue;
      }
      else return Color.Red;
    }

    public static int GetIndexForSensor(Sensor sensor)
    {
      if (sensor is BearingSensor)
      {
        return (int)((sensor as BearingSensor).landmarkID) + 1;
      }
      if (sensor is BearingSensorAmbiguous)
      {
        return 7;
      }
      else return 8;
    }
  }
}
